package com.it;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class ClassPathXmlApplicationContext implements BeanFactory {
    private Map<String, Object> beanMap = new HashMap();

    public ClassPathXmlApplicationContext(String path) {
        try {
            //1.读取applicationContext文件 获取输入流
            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(path);
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
            //2.根据输入流拿到Document对象
            Document document = documentBuilder.parse(inputStream);
            //3.获取所有bean节点
            NodeList nodeList = document.getElementsByTagName("bean");
            //4.遍历
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                //元素节点
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element element = (Element) node;

                    String id = element.getAttribute("id");
                    String className = element.getAttribute("class");
                    //创建实例对象
                    Object obj = Class.forName(className).newInstance();
                    beanMap.put(id, obj);
                }
            }
            //解析成员属性.设置值
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                //元素节点
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element element = (Element) node;
                    String beanId = element.getAttribute("id");
                    NodeList childList = element.getChildNodes();
                    for (int j = 0; j < childList.getLength(); j++) {
                        Node childNode = childList.item(j);
                        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                            Element childElement = (Element) childNode;
                            //成员变量名字
                            String propertyName = childElement.getAttribute("propertyName");
                            //对应的beanId
                            String propertyRef = childElement.getAttribute("propertyRef");

                            Object object = beanMap.get(beanId);
                            //得到成员属性实例的对象
                            Object propertyObject = beanMap.get(propertyRef);
                            //设置对象中成员属性的值
                            Field field = object.getClass().getField(propertyName);
                            field.setAccessible(true);
                            field.set(object, propertyObject);
                        }
                    }


                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public Object getBean(String id) {
        return beanMap.get(id);
    }
}
